﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using HRM.BusinessLogic;
using System.IO;
using HRM.Utilities;
using System.Xml;

namespace HRM
{
    public partial class frmToKhaiA01TSOnline : Form
    {
        string _selectedID = "";
        string _SoBHXH = "";
        RoleRightInfo listRole;
        DataTable dtSource;
        string _rpType = "";

        public frmToKhaiA01TSOnline()
        {
            InitializeComponent();
            listRole = UIParamater.Quyen_DeNghiCapSoBH;
            BuildVoucherGrid("");
            //LoadList(dtQTBHTrongKy,_Type);
            dgvList.ReadOnly = false;
            //_rpType = _Type;
            FormGlobals.Control_SetDefault(toolStrip1, btnHienThi_Click);
        }

        private void GetSelectedID()
        {
            dgvList.EndEdit();
            _selectedID = "";
            foreach (DataGridViewRow row in dgvList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                    _selectedID += row.Cells["Employee_ID"].Value.ToString() + ",";
            }
        }

        private string GetSoSoBHXHChecked()
        {
            dgvList.EndEdit();
            string _SoSoBHXH = "";
            foreach (DataGridViewRow row in dgvList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                    _SoSoBHXH += row.Cells["Detail_InsBookNo"].Value.ToString() + ",";
            }
            return _SoSoBHXH;
        }


        private void BuildVoucherGrid(string _Type)
        {
            dgvList.Columns.Clear();
            //Thêm cột Checkbox
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.Name = "Check";
            col.HeaderText = "Chọn";
            col.Width = 50;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvList.Columns.Add(col);
            col.Visible = true;
            //
            UISetting.AddColumn(ref dgvList, "Detail_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã NV", "", 30, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_DocumentID", "ID Ctừ", "C", 20, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_InsBookNo", "Số sổ BH", "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", "Họ và tên", "L", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_FromDate", "Từ tháng", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_ToDate", "Đến tháng", "C", 90, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_TotalMonth", "Số tháng", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_InsTypeCode", "PA", "R", 40, true, true);
            //if (_Type == "ML")
            //{
                UISetting.AddColumn(ref dgvList, "Detail_NewAmount", "ML mới", "R", 80, true, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldAmount", "ML cũ", "R", 80, true, true);
            //}
            //else if (_Type == "HS")
            //{
                UISetting.AddColumn(ref dgvList, "Detail_NewBasicFactor", "HSL mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewPositionFactor", "Cvụ mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewOverFactor", "TNVK mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewJobFactor", "TNN mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewAreaFactor", "KV mới", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldBasicFactor", "HSL cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldPositionFactor", "Cvụ cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldOverFactor", "TNVK cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldJobFactor", "TNN cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldAreaFactor", "KV cũ", "R", 80, false, true);
                UISetting.AddColumn(ref dgvList, "Detail_NewTotal", "Mức mới", "R", 80, true, true);
                UISetting.AddColumn(ref dgvList, "Detail_OldTotal", "Mức cũ", "R", 80, true, true);
            //}

            UISetting.AddColumn(ref dgvList, "BSTang_BHXH", "BS.Tăng BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHXH", "BS.Giảm BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSTang_BHYT", "BS.Tăng BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHYT", "BS.Giảm BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSTang_BHTN", "BS.Tăng BHTN", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "BSGiam_BHTN", "BS.Giảm BHTN", "R", 60, true, true);

            UISetting.AddColumn(ref dgvList, "Detail_SocialRate", "%BHXH", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_MedicalRate", "%BHYT", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Detail_UnemployedRate", "%BHTN", "R", 60, true, true);

            UISetting.AddColumn(ref dgvList, "Detail_Type", "HS/ML", "R", 60, false, true);
            UISetting.AddColumn(ref dgvList, "Detail_TakeCard", "Thu thẻ", "R", 60, false, true);

            UISetting.AddColumn(ref dgvList, "Document_BasicSalary", "MLTT", "R", 60, true, true);
        }

        private void LoadList(DataTable dtQTBHTrongKy,string _Type)
        {
            dgvList.Rows.Clear();
            int Counter = 0;
            dtSource = null;
            dtSource = dtQTBHTrongKy.Clone();
            
            if(dtQTBHTrongKy.Columns.Contains("Detail_InsTypeCode"))
            for (int i = 0; i < dtQTBHTrongKy.Rows.Count; i++)
            {
                string PA = dtQTBHTrongKy.Rows[i]["Detail_InsTypeCode"].ToString();
                if ("TC,TD,TM,AD".Contains(PA))
                {
                    DataRow drTmp = dtSource.NewRow();
                    foreach (DataColumn dc in dtSource.Columns)
                        drTmp[dc] = dtQTBHTrongKy.Rows[i][dc.ColumnName];
                    dtSource.Rows.Add(drTmp);
                    
                    //
                    dgvList.Rows.Insert(Counter, 1);
                    dgvList.Rows[Counter].Cells["Detail_ID"].Value = dtQTBHTrongKy.Rows[i]["Detail_ID"].ToString();
                    dgvList.Rows[Counter].Cells["Employee_ID"].Value = dtQTBHTrongKy.Rows[i]["Employee_ID"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_DocumentID"].Value = dtQTBHTrongKy.Rows[i]["Detail_DocumentID"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_InsBookNo"].Value = dtQTBHTrongKy.Rows[i]["Detail_InsBookNo"].ToString();
                    dgvList.Rows[Counter].Cells["Employee_FullName"].Value = dtQTBHTrongKy.Rows[i]["Employee_FullName"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_FromDate"].Value = Convert.ToDateTime(dtQTBHTrongKy.Rows[i]["Detail_FromDate"]).ToString("MM/yyyy");
                    dgvList.Rows[Counter].Cells["Detail_ToDate"].Value = Convert.ToDateTime(dtQTBHTrongKy.Rows[i]["Detail_ToDate"]).ToString("MM/yyyy");
                    dgvList.Rows[Counter].Cells["Detail_InsTypeCode"].Value = dtQTBHTrongKy.Rows[i]["Detail_InsTypeCode"].ToString();

                    //if (_Type == "ML")
                    //{
                        dgvList.Rows[Counter].Cells["Detail_NewAmount"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAmount"]).ToString("#,##0");
                        dgvList.Rows[Counter].Cells["Detail_OldAmount"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAmount"]).ToString("#,##0");
                    //}
                    //else if (_Type == "HS")
                    //{
                        dgvList.Rows[Counter].Cells["Detail_NewBasicFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewBasicFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewPositionFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewPositionFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewOverFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewOverFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewJobFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewJobFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewAreaFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAreaFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldBasicFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldBasicFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldPositionFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldPositionFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldOverFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldOverFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldJobFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldJobFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldAreaFactor"].Value = Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAreaFactor"]).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_NewTotal"].Value = (Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewBasicFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewPositionFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewOverFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewJobFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_NewAreaFactor"])).ToString(UIParamater.Cham3So);
                        dgvList.Rows[Counter].Cells["Detail_OldTotal"].Value = (Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldBasicFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldPositionFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldOverFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldJobFactor"]) + Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_OldAreaFactor"])).ToString(UIParamater.Cham3So);
                    //}


                    dgvList.Rows[Counter].Cells["BSTang_BHXH"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHXH"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHXH"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHXH"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHXH"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHXH"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSTang_BHYT"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHYT"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHYT"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHYT"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHYT"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHYT"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSTang_BHTN"].Value = dtQTBHTrongKy.Rows[i]["BSTang_BHTN"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSTang_BHTN"]).ToString(UIParamater.Phay2So);
                    dgvList.Rows[Counter].Cells["BSGiam_BHTN"].Value = dtQTBHTrongKy.Rows[i]["BSGiam_BHTN"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["BSGiam_BHTN"]).ToString(UIParamater.Phay2So);

                    dgvList.Rows[Counter].Cells["Detail_SocialRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_SocialRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_SocialRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_MedicalRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_MedicalRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_MedicalRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_UnemployedRate"].Value = dtQTBHTrongKy.Rows[i]["Detail_UnemployedRate"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Detail_UnemployedRate"]).ToString(UIParamater.Cham3So);
                    dgvList.Rows[Counter].Cells["Detail_Type"].Value = dtQTBHTrongKy.Rows[i]["Detail_Type"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_TakeCard"].Value = dtQTBHTrongKy.Rows[i]["Detail_TakeCard"].ToString();
                    dgvList.Rows[Counter].Cells["Detail_TotalMonth"].Value = Convert.ToInt32(dtQTBHTrongKy.Rows[i]["Detail_TotalMonth"]).ToString();

                    dgvList.Rows[Counter].Cells["Document_BasicSalary"].Value = dtQTBHTrongKy.Rows[i]["Document_BasicSalary"] == DBNull.Value ? "" : Convert.ToDecimal(dtQTBHTrongKy.Rows[i]["Document_BasicSalary"]).ToString(UIParamater.Phay2So);
                    
                    Counter++;
                }
            }
            lblTongSo.Text = lblTongSo.Text + Counter.ToString();
        }

        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dgvList.Rows)
            {
                dr.Cells["Check"].Value = true;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dgvList.Rows)
            {
                dr.Cells["Check"].Value = false;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                string SoBHXH = _SoBHXH != "" ? _SoBHXH : "Chờ cấp số";

                ////Gọi nhiều lần để show ra nhiều report
                //foreach (string strID in _selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                //{
                //if (_selectedID.StartsWith(","))
                //    _selectedID = _selectedID.Substring(1);
                //if (_selectedID.EndsWith(","))
                //    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);
                //    DataTable dtRetVal = GetDataTable2Report(dtSource, _selectedID);
                //    frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_Maumoi", _rpType, dtRetVal, new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.MaxValue.ToString(), SoBHXH });
                //    f.Show(this);
                //}

                //Show 1 lần với subreport
                //foreach (string strID in _selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                //{
                if (_selectedID.StartsWith(","))
                    _selectedID = _selectedID.Substring(1);
                if (_selectedID.EndsWith(","))
                    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);
                DataTable dtRetVal = GetDataTable2Report(dtSource, _selectedID);
                //frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_Maumoi", _rpType, dtRetVal, new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), DateTime.MaxValue.ToString(), SoBHXH });
                frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_TK1_TS", new string[] { _selectedID });
                f.Show(this);
                //}
                //
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private DataTable GetDataTable2Report(DataTable dtSource,string _selectedID)
        {
            DataTable dtRetVal = dtSource.Clone();
            if (_selectedID.StartsWith(","))
                _selectedID = _selectedID.Substring(1);
            if (_selectedID.EndsWith(","))
                _selectedID = _selectedID.Substring(0, _selectedID.LastIndexOf(","));


            string Filter = "Employee_ID IN (" + _selectedID + ")";
            foreach (DataRow MyDataRow in dtSource.Select(Filter))
	            dtRetVal.ImportRow(MyDataRow);
            return dtRetVal;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                if (_selectedID.StartsWith(","))
                    _selectedID = _selectedID.Substring(1);
                if (_selectedID.EndsWith(","))
                    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);

                //foreach(string SoBHXH in _strSoSoBHXH.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries))
                //{
                frmPreviewReport f = new frmPreviewReport("InPhuLucBHXH_NhieuMau", new string[] { _selectedID });
                    f.Show(this);
                //}
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            int iKy, iThang, iNam;
            Int32.TryParse(txtKy.Text, out iKy);
            Int32.TryParse(txtThang.Text, out iThang);
            Int32.TryParse(txtNam.Text, out iNam);
            //
            DataTable dt = Detail_Controller.Instance().Detail_Get4TK01(iKy, iThang, iNam, Program.intBranchID);

            FormGlobals.App_ProgressShow(delegate() { 
                LoadList(dt, "");
            },"Đang tải dữ liệu...");
        }

        //Xuất ra word
        private void btnExportWord_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //
            string templateFile = "";
            DataSet ds = null;
            //

            string sFilePath = Util.CreateFolderTemplate();
            string sXLSFilePath = "";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.doc)|*.doc";
            sfd.InitialDirectory = sFilePath != "" ? sFilePath : Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            sfd.FileName = "TK1-TS_10103N_" + DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.ToString("hhMMssfff") + ".doc";
            if (sfd.ShowDialog(this) == DialogResult.OK)
            {
                sXLSFilePath = sfd.FileName;
            }
            else
                return;


            bool DongHeSo = false;
            FormGlobals.App_ProgressShow(delegate()
            {
                //
                if (_selectedID.StartsWith(","))
                    _selectedID = _selectedID.Substring(1);
                if (_selectedID.EndsWith(","))
                    _selectedID = _selectedID.Substring(0, _selectedID.Length - 1);
                //

                string sEmpId = "(" + _selectedID + ")";
                DataTable dtNhanSu = Employee_Controller.Instance().Employee_GetReport(sEmpId);
                dtNhanSu.Columns.Add("DiaChiLH", typeof(string));
                dtNhanSu.Columns.Add("PhuongLH", typeof(string));
                dtNhanSu.Columns.Add("QuanLH", typeof(string));
                dtNhanSu.Columns.Add("ThanhPhoLH", typeof(string));
                dtNhanSu.Columns.Add("DiaChiHK", typeof(string));
                dtNhanSu.Columns.Add("PhuongHK", typeof(string));
                dtNhanSu.Columns.Add("QuanHK", typeof(string));
                dtNhanSu.Columns.Add("ThanhPhoHK", typeof(string));
                dtNhanSu.Columns.Add("DiaChiKS", typeof(string));
                dtNhanSu.Columns.Add("PhuongKS", typeof(string));
                dtNhanSu.Columns.Add("QuanKS", typeof(string));
                dtNhanSu.Columns.Add("ThanhPhoKS", typeof(string));

                dtNhanSu.Columns.Add("Ngay");
                dtNhanSu.Columns.Add("Thang");
                dtNhanSu.Columns.Add("Nam");
                //Xét địa chỉ liên hệ mới, nếu null hoặc split không đủ 4 trường thì lấy theo địa chỉ cũ
                
                for (int i = 0; i < dtNhanSu.Rows.Count; i++)
                {
                    dtNhanSu.Rows[i]["FullName"] = dtNhanSu.Rows[i]["FullName"].ToString().ToUpper();

                    DongHeSo = false;
                    //Trên report xuất word chỉ fill đc lương hoặc hệ số => cần update PD_BasicFactor cho PD_BasicSalary nếu đóng hệ số
                    if (Globals.Object_GetValue<decimal>(dtNhanSu.Rows[i]["PD_BasicFactor"]) > 0 &&
                        Globals.Object_GetValue<decimal>(dtNhanSu.Rows[i]["PD_BasicSalary"]) <= 0)
                    {
                        dtNhanSu.Rows[i]["PD_BasicSalary"] = Globals.Object_GetValue<decimal>(dtNhanSu.Rows[i]["PD_BasicFactor"]).ToString(UIParamater.Cham3So);
                    }
                   

                    //

                    if (dtNhanSu.Rows[i]["Employee_AddressLH"].ToString() != "")
                    {
                        string[] AddressLH = dtNhanSu.Rows[i]["Employee_AddressLH"].ToString().Split(new char[] { '|' });
                        if (AddressLH.Length == 4)
                        {
                            dtNhanSu.Rows[i]["DiaChiLH"] = AddressLH[0];
                            dtNhanSu.Rows[i]["PhuongLH"] = AddressLH[1];
                            dtNhanSu.Rows[i]["QuanLH"] = AddressLH[2];
                            dtNhanSu.Rows[i]["ThanhPhoLH"] = AddressLH[3];
                        }
                        else
                            dtNhanSu.Rows[i]["DiaChiLH"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];
                    }
                    else
                        dtNhanSu.Rows[i]["DiaChiLH"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];
                    //Xét địa chỉ hộ khẩu mới, nếu null hoặc split không đủ 4 trường thì lấy theo địa chỉ cũ
                    if (dtNhanSu.Rows[i]["Employee_AddressHK"].ToString() != "")
                    {
                        string[] AddressHK = dtNhanSu.Rows[i]["Employee_AddressHK"].ToString().Split(new char[] { '|' });
                        if (AddressHK.Length == 4)
                        {
                            dtNhanSu.Rows[i]["DiaChiHK"] = AddressHK[0];
                            dtNhanSu.Rows[i]["PhuongHK"] = AddressHK[1];
                            dtNhanSu.Rows[i]["QuanHK"] = AddressHK[2];
                            dtNhanSu.Rows[i]["ThanhPhoHK"] = AddressHK[3];
                        }
                        else
                            dtNhanSu.Rows[i]["PhuongHK"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];
                    }
                    else
                        dtNhanSu.Rows[i]["PhuongHK"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];
                    //Xét địa chỉ khai sinh mới, nếu null hoặc split không đủ 4 trường thì lấy theo địa chỉ cũ
                    if (dtNhanSu.Rows[i]["Employee_AddressKS"].ToString() != "")
                    {
                        string[] AddressKS = dtNhanSu.Rows[i]["Employee_AddressKS"].ToString().Split(new char[] { '|' });
                        if (AddressKS.Length == 4)
                        {
                            dtNhanSu.Rows[i]["DiaChiKS"] = AddressKS[0];
                            dtNhanSu.Rows[i]["PhuongKS"] = AddressKS[1];
                            dtNhanSu.Rows[i]["QuanKS"] = AddressKS[2];
                            dtNhanSu.Rows[i]["ThanhPhoKS"] = AddressKS[3];
                        }
                        else
                            dtNhanSu.Rows[i]["DiaChiKS"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];
                    }
                    else
                        dtNhanSu.Rows[i]["DiaChiKS"] = dtNhanSu.Rows[i]["Employee_Address"] == null ? "" : dtNhanSu.Rows[i]["Employee_Address"];

                    //
                    dtNhanSu.Rows[i]["Ngay"] = DateTime.Now.ToString("dd");
                    dtNhanSu.Rows[i]["Thang"] = DateTime.Now.ToString("MM");
                    dtNhanSu.Rows[i]["Nam"] = DateTime.Now.ToString("yyyy");

                    //Giới tính
                    //if (dtNhanSu.Rows[i]["Employee_Sex"] != DBNull.Value && Globals.Object_GetValue<bool>(dtNhanSu.Rows[i]["Employee_Sex"]) == true)
                    //    dtNhanSu.Rows[i]["Employee_Sex"] = "Nam";
                    //else
                    //    dtNhanSu.Rows[i]["Employee_Sex"] = "Nữ";
                }

                //export by mail merge
                templateFile = Path.Combine(Application.StartupPath, "ExportTemplate\\TK1-TS.xml");
                ds = new DataSet();
                DataTable dtData = dtNhanSu.Copy();
                dtData.TableName = "tblTK01";

                //Lấy phụ lục
                
                //
                ds.Tables.Add(dtData);
                //ds.Tables.Add(dtPhuLuc);
            }, "Đang lấy dữ liệu");
            //
            string sTempFile = "";
            FormGlobals.App_ProgressShow(delegate()
            {
                HRM.Utilities.MSWordUtil.CallBack += new MSWordUtil.CallbackFill((XmlNode fieldNode, string field, object data)=>{
                    switch (field.ToUpper().Replace(".",""))
                    {
                        case "TBLTK01PD_BASICSALARY"://mức lương
                        case "TBLTK01PD_POSITIONFACTOR"://PC Chuc vu
                        case "TBLTK01PD_OVERFACTOR"://PC vuot khung
                        case "TBLTK01PD_JOBFACTOR"://PC Nghe
                        case "TBLTK01PD_OVERALLOWANCE": //PC khac
                            decimal val = 0;
                            Decimal.TryParse(data.ToString(), out val);
                            fieldNode.InnerText = val > 1000 ? val.ToString(UIParamater.Phay2So) : val.ToString(UIParamater.Cham3So);
                            break;
                    }
                });
                //
                sTempFile = HRM.Utilities.MSWordUtil.CreateTemplateDoc(templateFile, ds);
            }, "Đang xử lý mẫu word");

            File.Move(sTempFile, sXLSFilePath);
            //Process.Start(sFilePath);

            System.Diagnostics.Process.Start(sXLSFilePath);//
        }

    }
}
